﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace DYSales.Views.Stock
{
    public partial class cwin_unStock : ChildWindow
    {
        internal long pdid;
        internal decimal max;
        public cwin_unStock()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (tb_退货数量.Value <= 0)
            {
                tb_msg.Text = "退货数量不能于少1";
                return;
            }
            if (tb_退货数量.Value > Convert.ToDouble(max))
            {
                tb_msg.Text = "仓库库存量少于你要退货的数量";
                return;
            }
            dm.getDS().GetST(lo =>
            {
                var pd = dm.getDS().PdTables.FirstOrDefault(p => p.id.Equals(pdid));
                if (pd != null)
                {
                    pd.p_库存数量 -= Convert.ToDecimal(tb_退货数量.Value);

                    var unst = new Web.StockDel();
                    unst.st_操作员 = WebContext.Current.User.Name;
                    unst.st_进货价 = pd.p_进货价;
                    unst.st_商品编号 = pd.p_商品编号;
                    unst.st_商品名称 = pd.p_商品名称;
                    unst.st_退货时间 = lo.Value;
                    unst.st_退货数量 = Convert.ToDecimal(tb_退货数量.Value);
                    dm.getDS().StockDels.Add(unst);

                    dm.getDS().SubmitChanges(le =>
                    {
                        this.DialogResult = true;
                    }, null);
                }
                else
                {
                    tb_msg.Text = "该商品不存";
                }
            }, null);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

